package io.renren.modules.repair.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.repair.entity.RepairEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.renren.modules.repair.entity.req.UserRepairListReq;
import io.renren.modules.repair.mapper.RepairMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 
 * 
 * @author LL
 * @email wdlyl@163.com
 * @date 2022-04-12 14:48:24
 */
@Repository
public class RepairDao {
    
    private final RepairMapper repairMapper;
    
    @Autowired
    public RepairDao(RepairMapper repairMapper) {
        this.repairMapper = repairMapper;
    }
    
    public Page<RepairEntity> selectRepairList(UserRepairListReq req){
        Page<RepairEntity> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<RepairEntity> wrapper = new QueryWrapper<>();
        wrapper.eq((null != req.getApplyId()), "apply_id", req.getApplyId())
                .eq((null != req.getUserId()), "user_id", req.getUserId())
                .eq((null != req.getCarId()), "car_id", req.getCarId())
                .eq((null != req.getShopId()), "shop_id", req.getShopId())
                .gt((null != req.getBeginTime()), "begin_time", req.getBeginTime())
                .lt((null != req.getEndTime()), "end_time", req.getEndTime())
                .eq(StringUtils.isNoneEmpty(req.getRepaired()), "repaired", req.getRepaired())
                .eq(StringUtils.isNoneEmpty(req.getPaid()), "paid", req.getPaid())
                .ge((null != req.getMinRepairPrice()), "repair_price", req.getMinRepairPrice())
                .le((null != req.getMaxRepairPrice()), "repair_price", req.getMaxRepairPrice());
        return repairMapper.selectPage(page, wrapper);
    }
    
    public void deleteRepairByIds(List<Integer> ids){
        repairMapper.deleteBatchIds(ids);
    }
    
    public RepairEntity selectRepairById(Integer repairId) {
        return repairMapper.selectById(repairId);
    }
    
    public void save(RepairEntity repair) {
        repairMapper.updateById(repair);
    }
}
